<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\UserBankModel;
use App\Model\UserWidthwalModel;
use Illuminate\Support\Facades\DB;

class UserBankDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = UserBankModel::query();

        $query->when(checkIsHasField($params,'user_bank_id'),function ($query)use ($params){
            $query->where('user_bank_id',$params['user_bank_id']);
        });

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });


        return $query;
    }

    /**
     * @throws SysExceptions
     */
    public static function add_user_bank($params = [])
    {
        checkForData(
            ['bank_id','user_id','name','card_no','bank_kai'],
            ['请选择银行','请选择用户','请输入姓名','请输入银行卡号','请选择开户银行'],
            $params
        );
        $params['bank_json_data'] = json_encode(BankDao::info([
            'bank_id' => $params['bank_id'],
        ]),JSON_UNESCAPED_UNICODE);
        if(self::builder_query()->create($params) === false){
            self::error('下单失败');
        }
    }

    public static function update_user_bank($params = [])
    {
        checkForData(
            ['bank_id','user_id','name','card_no','bank_kai','user_bank_id'],
            ['请选择银行','请选择用户','请输入姓名','请输入银行卡号','请选择开户银行','请选择银行卡'],
            $params
        );
        $params['bank_json_data'] = json_encode(BankDao::info([
            'bank_id' => $params['bank_id'],
        ]),JSON_UNESCAPED_UNICODE);
        if(self::builder_query(['user_bank_id'=>self::deleteParamId($params,'user_bank_id')])->update(self::filterFields($params,new UserBankModel())) === false){
            self::error('下单失败');
        }
    }

    /**
     * @throws SysExceptions
     */
    public static function delete_user_bank($params = [])
    {
        checkForData(
            ['user_bank_id'],
            ['请选择银行卡'],
            $params
        );
        if(self::builder_query(['user_bank_id'=>self::deleteParamId($params,'user_bank_id')])->delete() === false){
            self::error('删除失败');
        }
    }

    public static function user_bank_cards(array $array = [])
    {
        return self::builder_query($array)->get();
    }

    /**
     * @throws SysExceptions
     */
    public static function user_create_widthwal(array $params)
    {
        checkForData(
            ['price', 'user_card_id', 'user_id',],
            ['请输入价格','请选择银行卡','请选择用户'],
            $params
        );
        if($params['price'] < 100){
            self::error('最低提现100');
        }
        $userInfo = UsersDao::user_info([
            'user_id' => $params['user_id'],
        ]);
        if($userInfo['balance'] < $params['price']){
            self::error('用户余额不足!');
        }
        $params['bank_card_json'] = UserBankDao::info([
            'user_card_id'=>$params['user_card_id']
        ]);
        DB::beginTransaction();
        try {
            UserWidthwalModel::query()->create($params);
            BillsDao::moneyUpdate($params['user_id'],-($params['price']),'申请提现');
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error('失败'.$exception->getMessage());
        }
    }

    private static function info(array $array)
    {
        return self::builder_query([
            'user_card_id'=>$array['user_card_id']
        ])->first();
    }
}
